﻿using System.Reflection;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using Spotted.WebApi.Core;

namespace Spotted.WebApi
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        private IKernel _currentKernel;
        public IKernel CurrentKernel
        {
            get
            {
                return _currentKernel ?? (_currentKernel = new StandardKernel(
                                                               new WebApiNinjectModule(Assembly.GetExecutingAssembly()),
                                                               new DataProviderNinjectModule()));
            }
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            // set dependency resolver, think Custom factory from MVC
            CurrentKernel.Settings.ActivationCacheDisabled = true;
            GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(CurrentKernel);

            // add additional formatters
            GlobalConfiguration.Configuration.Formatters.Add(new BinaryMediaTypeFormatter());
        }
    }
}